<template>
	<view class="mask" v-if="isShow">
		<view class="update-content">
			<view class="step1" v-if="step == 1">
				<view class="step1-content">
					<view class="step1-title">
						版本更新
					</view>
					新版本：<text class="update-version">{{updateData.versionCode}}</text></br>
					<scroll-view scroll-y="true">
						<view 
						v-for="(dec,index) in updateData.description"
						:key = "index"
						class="update-description">{{index+1}}. {{dec}}</view>
					</scroll-view>
				</view>
				<view class="buttons">
					<button type="default" @click="closeModal" >取消</button>
					<button type="warn" @click="confirmWgt" >升级</button>
				</view>
			</view>
			
			<view class="step2" v-else>
				<view class="logo-block">
					<image src="../../static/108x108.png" mode="aspectFill" class="logo-img"></image>
				</view>
				<text>{{updateData.name}}</text>
				<cmd-progress :percent="percentSize" class="step2-progress"></cmd-progress>
				<text class="step2-text">正在下载安装中</text>
			</view>
			
		</view>
	</view>
</template>

<script>
	var _self = this
	export default {
		data() {
			return {
				isShow: false,
				step: 1,
				updateData: {},
				percentSize: 0,
				fileList: []
			}
		},
		created() {
			_self = this
		},
		methods: {
			show(updateData){
				this.updateData = updateData
				this.$nextTick(function(){
					_self.isShow = true
				})
			},
			closeModal() {
				this.isShow = false;
			},
			// 资源更新
			confirmWgt() {
				this.step = 0
				this.$nextTick(function(){
					_self.isShow = true
					_self.step = 2
				})
				this.downloadWgt();
			},
			// 整包更新
			confimApk(){
				this.downloadApk()
			},
			// 整包更新apk,直接打开url
			downloadApk(){
				plus.runtime.openURL(this.updateData.href)
			},
			// 下载资源热更新
			downloadWgt(){
				// 判断文件是否存在
				
				plus.io.resolveLocalFileSystemURL( "_doc/update/"+this.updateData.updateUrl.split("/").pop(), 
					function(entry ){
						console.log("成功",entry);
						_self.percentSize = 100
						// 直接安装
						_self.installWgt("_doc/update/"+_self.updateData.updateUrl.split("/").pop())
					},function(e){
						console.log("失败",e);
						let dark = plus.downloader.createDownload(_self.updateData.updateUrl,{filename:"_doc/update/"},(d,status) =>{
						    if ( status == 200 ) {   
								console.log("下载wgt成功：" + JSON.stringify(d.filename));
								// plus.nativeUI.alert("下载wgt成功1");
								if(d.filename){
									_self.installWgt(d.filename); // 安装wgt包
								}
						    } else {  
						        // console.log("下载wgt失败！");  
						        plus.nativeUI.alert("下载wgt失败！");  
						    }  
						    // plus.nativeUI.closeWaiting();  
						});
									
						dark.start();
						dark.addEventListener("statechanged",(download,status) =>{
							if(download.downloadedSize > 0){
								 _self.percentSize = parseInt(download.downloadedSize/download.totalSize * 100);
								 // console.log(_self.percentSize,"百分比")
							}
							if(download.state == 4){
								_self.isShowLoading = false;
								_self.$emit("finshUpdate",true)
							}
						},false);
					}
				)
				// plus.nativeUI.showWaiting("下载wgt文件...");  
			
			},
			// 安装更新包
			installWgt(path){
				console.log("安装包路径"+path)
				// plus.nativeUI.showWaiting("安装2"+path);  
				plus.runtime.install(path,{},function(){  
				    plus.nativeUI.closeWaiting();  
					// plus.runtime.restart();  
				    plus.nativeUI.alert("点击刷新",function(){  
				        plus.runtime.getProperty(plus.runtime.appid,inf =>{
				        	// console.log("当前版本"+inf.version)
				        	// console.log("最新版本"+_self.updateData.versionCode)
				        	if(inf.version != _self.updateData.versionCode){
				        		_self.isShow = false
				        		_self.step = 1
				        		uni.showToast({
				        			icon:"none",
				        			title:"更新失败,用户取消"
				        		})
				        	}else{
				        		console.log("已自动删除安装包文件")
				        		uni.removeSavedFile({
				        			filePath: path,
				        			complete: function (res) {
				        				plus.runtime.restart();
				        			  // console.log(res);
				        			}
				        		});
				        	}
				        })  
				    }); 
				},function(e){  
				    plus.nativeUI.closeWaiting();  
				    console.log("安装wgt文件失败["+e.code+"]："+e.message);  
				    plus.nativeUI.alert("安装wgt文件失败["+e.code+"]："+e.message);  
				});  
			},
		}
	}
</script>

<style>
	.mask {
		background-color: rgba(0,0,0,0.1);
		position: fixed;
		top: 0;
		width: 100%;
		height: 100%;
		z-index: 10;
		display: flex;
		align-items: center;
	}
	
	.update-content{
		width: 80%;
		border-radius: 20rpx;
		margin-left: 10%;
		background-color: #ffffff;
	}
	scroll-view{
		height: 380rpx;
		margin-top: 20rpx;
		padding-top: 30rpx 0;
		font-size: 28rpx;
		color: #4f4f4f;
	}
	.step1{
		padding: 50rpx;
		padding-top: 0;
		flex-direction: column;
		justify-content: space-between;
	}
	.step1-title{
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-weight: bold;
		font-size: 40rpx;
	}
	.step1-content{
		margin: 50rpx 0 ;
	}
	.update-version{
		font-size: 40rpx;
		font-weight: 600;
		height: 50rpx;
		line-height: 50rpx;
		color: #ff0000;
		font-size: 36rpx;
	}
	.buttons{
		margin-top: 50rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
	.step2{
		display: flex;
		flex-direction: column;
		text-align: center;
		justify-content: space-around;
		padding: 50rpx;
	}
	.step2-progress{
		margin: 50rpx 0;
	}
	
	.logo-img{
		width: 120rpx;
		height: 120rpx;
	}
	
</style>
